package com.gorkr;

import org.junit.jupiter.api.Test;

/**
 * @author gorkr
 * @date 2022/09/13 14:14
 **/
public class E69Sqrtx {

    public int mySqrt2(int x) {
        if (x == 0) {
            return 0;
        } else if (x == 1) {
            return 1;
        }
        long left = 0, right = x / 2, mid = 0;
        while (left < right) {

            mid = (int) ((left + right) / 2) + 1;
            if (mid * mid < x) {
                left=mid;

            } else if (mid * mid == x) {
                return (int) mid;
            } else {
                right = mid - 1;
            }
        }
        return (int)mid;


    }


    @Test
    public void test() {
        System.out.println(mySqrt2(
            8));
    }

}
    